//
//  KLGlobalSearchViewController.m
//  KunLun
//
//  Created by Mac on 2018/1/17.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "KLGlobalSearchViewController.h"
#import "KLMineShopTableViewCell.h"
@interface KLGlobalSearchViewController ()
@property(nonatomic, strong) STNoresultView                     *noResultView;
@property(nonatomic, strong) NSMutableArray <KLGoodDetailModel*>                    *dataSouce;
@property(nonatomic, strong) UITextField                     *serachTextFiled;
@end

@implementation KLGlobalSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendGoodListRequest];
    }];
    self.noResultView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, self.tableView.tableHeaderView.height + 100, UIScreenWidth, 350)
                                                        title:@"暂无商品"
                                                  buttonTitle:@""
                                                 buttonHandle:^(NSString *titleString) {
                                                     
                                                 }];
    self.noResultView.hidden = YES;
    [self.tableView addSubview:self.noResultView];
    
    [self configSubView];
    [self configLeftItem];
    [self reloadData];
    // Do any additional setup after loading the view.
}
- (void)reloadData{
    self.noResultView.hidden = self.dataSouce.count;
}
#pragma mark --subView
- (void)configSubView{
   
    //配置头部
    _serachTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth * 0.66, 36)];
    _serachTextFiled.placeholder = @"请输入关键字搜索";
    
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 7, 36, 36)
                                                     title:@""
                                                titleColor:KL_secendTextColor
                                                 titleFont:16
                                              cornerRadius:15
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"img_nav_search_normal"]];
    buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    _serachTextFiled.leftView = buyButton;
    _serachTextFiled.leftViewMode = UITextFieldViewModeAlways;
    _serachTextFiled.backgroundColor = [UIColor clearColor];
    [_serachTextFiled st_setBorderWith:1 borderColor:FlatWhite cornerRadius:5];
    
    self.navigationItem.titleView = self.serachTextFiled;
    
    [self st_setRightItemWithTitle:@"搜索" titleColor:FlatWhite];
}
- (void)configLeftItem{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(back)  forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:[UIImage imageNamed:@"icon_返回"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"icon_返回"] forState:UIControlStateHighlighted];
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    btn.frame = CGRectMake(0, 0, 44, 44);
    self.navigationItem.leftBarButtonItem =  [[UIBarButtonItem alloc] initWithCustomView:btn];
}
- (void)back{
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)st_rightBarAction:(id)sender{
    [self sendGoodListRequest];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [KLMineShopTableViewCell CellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    KLMineShopTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[KLMineShopTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.dataSouce[indexPath.row];
    cell.changeButton.hidden = YES;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self dismissViewControllerAnimated:YES completion:^{
        KLGoodDetailViewController * vc = [KLGoodDetailViewController new];
        vc.goodID = self.dataSouce[indexPath.row].goodID.description;
        [self.currentVC.navigationController pushViewController:vc animated:YES];
    }];

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)sendGoodListRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/goods/goodslist"];
    
    [dic setObject:@(self.type) forKey:@"classification"];
    if (self.serachTextFiled.text.length) {
        [dic setObject:self.serachTextFiled.text forKey:@"name"];
    }
    
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 self.dataSouce = [NSMutableArray new];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 if ([dataDic.allKeys containsObject:@"list"]) {
                                                     NSArray * list = dataDic[@"list"];
                                                     for (NSDictionary * listdic in list) {
                                                         KLGoodDetailModel * model = [KLGoodDetailModel  mj_objectWithKeyValues:listdic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                 }
                                                 self.noResultView.hidden = self.dataSouce.count;
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 self.noResultView.hidden = self.dataSouce.count;
                                                 [self.tableView.mj_header endRefreshing];
                                                 
                                             }];
}

@end
